/*
 * Copyright (c) 2009. Silenus Consultoria, S.L.
 */
package es.silenus.detecta.model;

import java.util.HashSet;
import java.util.Set;


/**
 * Knowledge area. Persistent class, read-write.
 *
 * @author <a href="mailto:malonso@silenus-consultoria.es">Mariano Alonso</a>
 *
 * @since 22-mar-2009 11:04:04
 */
public class KnowledgeArea implements java.io.Serializable {
	/**
	 * The identifier.
	 */
	private int id;

	/**
	 * The knowledge are code, must be unique per organization.
	 */
	private String code;

	/**
	 * The description.
	 */
	private String description;

	/**
	 * The owner organization.
	 */
	private Organization organization;

	/**
	 * The paths associated with each knowledge area.
	 */
	private Set<KnowledgePath> paths;

/**
   * Constructor.
   */
	public KnowledgeArea() {
		paths = new HashSet<KnowledgePath>();
	}

	/**
	 * Retrieves the identifier.
	 *
	 * @return the identifier.
	 */
	public int getId() {
		return this.id;
	}

	/**
	 * Sets the identifier.
	 *
	 * @param id the identifier.
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * Retrieves the code.
	 *
	 * @return the code.
	 */
	public String getCode() {
		return this.code;
	}

	/**
	 * Sets the code.
	 *
	 * @param code the code.
	 */
	public void setCode(final String code) {
		this.code = code;
	}

	/**
	 * Retrieves the description.
	 *
	 * @return the description.
	 */
	public String getDescription() {
		return this.description;
	}

	/**
	 * Sets the description.
	 *
	 * @param description the description.
	 */
	public void setDescription(final String description) {
		this.description = description;
	}

	/**
	 * Retrieves the organization.
	 *
	 * @return the organization.
	 */
	public Organization getOrganization() {
		return this.organization;
	}

	/**
	 * Sets the organization.
	 *
	 * @param organization the organization.
	 */
	public void setOrganization(final Organization organization) {
		this.organization = organization;
	}

	/**
	 * Retrieves the knowledge paths.
	 *
	 * @return the knowledge paths.
	 */
	public Set<KnowledgePath> getPaths() {
		return this.paths;
	}

	/**
	 * Sets the knowledge paths.
	 *
	 * @param paths the knowledge paths.
	 */
	public void setPaths(final Set<KnowledgePath> paths) {
		this.paths = paths;
	}

	/**
	 * Adds a path.
	 *
	 * @param path a path.
	 */
	public void add(final KnowledgePath path) {
		getPaths().add(path);
		path.setArea(this);
	}

	/**
	 * Removes a path.
	 *
	 * @param path a path.
	 */
	public void remove(final KnowledgePath path) {
		getPaths().remove(path);
		path.setArea(null);
	}

	/**
	 * Checks if objects are equal.
	 *
	 * @param o other object.
	 *
	 * @return true if equal, false otherwise.
	 */
	@Override
	public boolean equals(Object o) {
		if(this == o) {
			return true;
		}

		if(!(o instanceof KnowledgeArea)) {
			return false;
		}

		KnowledgeArea that = (KnowledgeArea)o;

		if(getId() != that.getId()) {
			return false;
		}

		return true;
	}

	/**
	 * Calculates the hash of this instance.
	 *
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		return id;
	}

	/**
	 * Stringifies this instance.
	 *
	 * @return the string representation.
	 */
	@Override
	public String toString() {
		final StringBuilder sb = new StringBuilder();

		sb.append("KnowledgeArea");
		sb.append("{id=").append(getId());
		sb.append(", code='").append(getCode()).append('\'');
		sb.append('}');

		return sb.toString();
	}
}
